clear
capture restore
set scheme cleanplots, perm

* SET DIRECTORIES
global home "..."

global data "$home/Data/"
global output "$home/Output/"

cd $home

********************************************************************************
*Create daily stock price dataset
use "${data}full_share_data_final", clear

*Drop unnecessary variables
keep gvkey cusip datadate cshoc prccd
order gvkey cusip datadate cshoc prccd

*Drop if missing share price
drop if prccd==.

rename datadate date

*Create day, month and year variables for merging
gen day = day(date)
gen month = month(date)
gen year = year(date)

*Drop data prior to sample period
drop if year<=1995

destring gvkey, gen(firmid)

*save "${rdata}share_price_all_days", replace

*Number observations within firm/cusip groupings
egen group = group(firmid cusip)
sort group date
by group: gen n = _n

*Merge on monetary policy shock variables & flag FOMC dates
merge m:1 year month day using "${data}MP_shocks_daily", keepusing(year month day)
gen fomc = (_merge==3)
drop if _merge==2
drop _merge

*To decrease size of dataset, next we drop observations not around an FOMC meeting date
xtset group n

*Calculate days between observations
gen between = date - L.date

*Only keep observations within a week of FOMC meeting (will drop more next)
keep if (fomc & between<=7)|(F.fomc & F.between<=7)|(L.fomc & between<=7)|(L2.fomc & between<=7 & L.between<=7)|(L3.fomc & between<=7 & L2.between<=7 & L.between<=7)

*On each date, calculate the shortest distance to previous date
*This is done to identify when previous trading day was, i.e. accounts for 
*weekends/holidays.
bysort date: egen min_between = min(between) 

*Drop if missing share price on a trading day
gen gap_flag = (between!=min_between)
drop if gap_flag

xtset group n 

*Calculate percentage change in firm share price
gen prccd_lag = L.prccd

*Only keep FOMC dates
keep if fomc

gen firm_stock_chg 		= (ln(prccd) - ln(prccd_lag)) * 100
label var firm_stock_chg "Firm-Level Daily Pct. Change in Stock Price"

drop if missing(firm_stock_chg)

*Drop unnecessary variables
drop n between min_between gap_flag group firmid

*Create 8-digit CUSIP consistent with ivol dataset
gen cusip_8 = substr(cusip,1,8)

save "${data}stk_daily_data", replace


********************************************************************************
********************************************************************************
*Read-in ivol data (contains all trading days for Compustat firms)
use "${data}ivol_final", clear

sort cusip_8 date

*Create quarters
gen 	qtr=1 if month>=1 & month<=3
replace qtr=2 if month>=4 & month<=6
replace qtr=3 if month>=7 & month<=9
replace qtr=4 if month>=10 & month<=12

***Merge on daily stock price change (FOMC days only)
merge 1:1 cusip_8 date using "${data}stk_daily_data"
drop if _merge==2
drop _merge 

rename gvkey gvkey_str

***Merge on firm characteristics
merge m:1 cusip_8 year qtr using "${data}Compustat_Quarterly_Final", keepusing(cusip cusip_8 year qtr fqtr age log_salesg_real_1lag size_assets_1lag current_total_1lag liquidity_1lag pcmq_1lag recpay2yq_1lag d2aq_1lag cash2sales_1lag ln_market_cap_q_1lag tobins_q_1lag lev_d2a_1lag lev_d2c_1lag lev_d2e_1lag lt_debt_share_1lag st_debt_share_1lag cshoq_1lag sic_2dig sector qtr_industry gvkey_str) update replace
drop if _merge==2
drop _merge
	
*Merge on VIX
merge m:1 month day year using "${data}vix"
drop if _merge==2
drop _merge	

*Multiply ivol by 100 to be consistent with VIX
foreach x in m1 m2 m3 m1545 m30 { 
	foreach y in ivol divol { 
	
		replace `y'_`x' = 100 * `y'_`x'
		capture replace `y'_`x'_lag = 100 * `y'_`x'_lag
			
	}
}

*Merge on MP shocks data
merge m:1 month day year using "${data}MP_shocks_daily"
*Drop dates before sample
drop if _merge==2
drop _merge

*Merge on MP shocks data
merge m:1 month day year using "${data}MP_shocks_intraday"
*Drop dates before sample
drop if _merge==2
drop _merge

replace unscheduled=0 if missing(unscheduled)
replace fomc=1 if !missing(mp2_d_shock)
replace unscheduled=1 if date==mdy(10,4,2019)

*Flag 1% outliers
foreach i in m1 m2 m3 m1545 m30 { 

	sum divol_`i', detail
	gen divol_`i'_outlier = ((divol_`i' < r(p1) | divol_`i' > r(p99)) & !missing(divol_`i'))

}

drop mpu10_chg mpu05_chg

drop if year==2020

***Flag the Top 100 liquid firms
gen fomc_nm = (!missing(divol_m1545) & fomc==1)
bysort cusip_8: egen fomc_count = sum(fomc_nm)
gen top100_flg = (fomc_count >=190 & !missing(fomc_count))

label var ivol_m1545 "ivol (15-45 days)"
label var divol_m1545 "change in ivol (15-45)"
label var vix "VIX"

drop pre_crisis post_crisis crisis_flg

gen 	pre_crisis = (date < date("01JUL2008","DMY"))

gen 	post_crisis = (date > date("30JUN2009","DMY")) & !missing(date)

gen 	crisis_flg = (date >= date("01JUL2008","DMY") & date <= date("30JUN2009","DMY"))

save "${data}final_daily_data", replace

collapse (mean) ivol_m1545 ivol_m30 ivol_m1 ivol_m2 ivol_m3  , by(year qtr gvkey_str) cw

save "${data}qtrly_ivol_data", replace

********************************************************************************
********************************************************************************
********************************************************************************




